package cn.joybreeze.common.bo;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName AdminUserDetails
 * @Description 登录用户，该类包括用户的 用户名 密码 权限
 * @Author Kjwaa
 * @Email 1135274290@qq.com
 * @Date 2021/5/13 20:55
 */
public class AdminUserDetails implements UserDetails {
    private UsmAdmin usmAdmin;

    public AdminUserDetails(UsmAdmin usmAdmin) {
        this.usmAdmin = usmAdmin;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> authorities = usmAdmin.getRoleList().stream()
                .map(role -> new SimpleGrantedAuthority("ROLE_"+role.getName()))
                .collect(Collectors.toList());
        System.out.println(authorities);
        return authorities;
    }

    @Override
    public String getPassword() {
        return usmAdmin.getPassword();
    }

    @Override
    public String getUsername() {
        return usmAdmin.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

}
